sortlistmodel: Make sort stable
authorBenjamin Otte <otte@redhat.com>
Wed, 11 Dec 2019 00:22:20 +0000 (01:22 +0100)
committerMatthias Clasen <mclasen@redhat.com>
Sat, 30 May 2020 23:26:44 +0000 (19:26 -0400)
The sort of the sortlistmodel is now stable with respect to the original
list model.

That means that if the sorter compares items as equal, the model
will make sure those items keep the order they were in in the original
model.

Or in other words: The model guarantees a total order based on the
item's position in the original model.

gtk/gtksortlistmodel.c

index 2fa9de9944fc3d3ca49e4af0e79b15587c72bf2c..5444ee74aeab872aacc93159ac26317908a5fd03 100644 (file)
@@ -181,8 +181,14 @@ _sort_func (gconstpointer item1,
 {
   GtkSortListEntry *entry1 = (GtkSortListEntry *) item1;
   GtkSortListEntry *entry2 = (GtkSortListEntry *) item2;
+  GtkOrdering result;
 
-  return gtk_sorter_compare (GTK_SORTER (data), entry1->item, entry2->item);
+  result = gtk_sorter_compare (GTK_SORTER (data), entry1->item, entry2->item);
+
+  if (result == GTK_ORDERING_EQUAL)
+    result = g_sequence_iter_compare (entry1->unsorted_iter, entry2->unsorted_iter);
+
+  return result;
 }
 
 static void